package com.ueboot.weishop.service.customer.impl;

import com.ueboot.weishop.entity.customer.CustomerInfoEntity;
import com.ueboot.weishop.entity.customer.CustomerReceiverEntity;
import com.ueboot.weishop.repository.customer.CustomerReceiverRepository;
import com.ueboot.weishop.service.customer.CustomerReceiverService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.ExampleMatcher;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * Created by Neel on 2017/9/24.
 */
@Slf4j
@Service
public class CustomerReceiverServiceImpl implements CustomerReceiverService {

    @Autowired
    private CustomerReceiverRepository repository;


    @Override
    public Page<CustomerReceiverEntity> findBy(Pageable pageable) {
        return repository.findAll(pageable);
    }

    /**
     * 根据关键字查询
     *
     * @param pageable
     * @param keyword
     * @return
     */
    @Override
    public Page<CustomerReceiverEntity> findBy(Pageable pageable, String keyword) {
        CustomerReceiverEntity entity = new CustomerReceiverEntity();
        ExampleMatcher example = ExampleMatcher.matching()
                .withIgnoreNullValues()
                .withMatcher("name", matcher -> matcher.contains())
                .withMatcher("remark", matcher -> matcher.contains());
        return repository.findAll(Example.of(entity, example), pageable);
    }

    @Override
    public CustomerReceiverEntity save(CustomerReceiverEntity s) {
        return repository.save(s);
    }

    @Override
    public CustomerReceiverEntity get(Long id) {
        return repository.findOne(id);
    }

    @Override
    public List<CustomerReceiverEntity> findByCustomerId(Long customerId) {
        return repository.findByCustomerId(customerId);
    }


    @Override
    public void delete(Long id) {
        repository.delete(id);
    }

    @Override
    public void delete(Long[] ids) {
        for (Long id : ids) {
            repository.delete(id);
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class, timeout = 30, propagation = Propagation.REQUIRED)
    public void updateUnDefaultByCustomerId(Long customerId, Boolean unDefault) {
        repository.updateUnDefaultByCustomerId(customerId,unDefault);
    }

	@Override
	public CustomerReceiverEntity findById(Long addressId) {
		return repository.findById(addressId);
	}

}
